package cn.zjgem.real.report.dto;



import cn.zjgem.real.report.enums.AppHttpCodeEnum;

import java.io.Serializable;


/**
 * 通用的结果返回类
 *
 * @param <T>
 */
public class ResponseResult<T> implements Serializable {


    private Integer code;
    private String msg;
    private T data;

    public ResponseResult() {
        this.code = 200;
    }

    public ResponseResult(Integer code, Boolean success, T data) {
        this.code = code;

        this.data = data;
    }

    public ResponseResult(Integer code, Boolean success, String msg, T data) {
        this.code = code;

        this.msg = msg;
        this.data = data;
    }

    public ResponseResult(Integer code, Boolean success, String msg) {
        this.code = code;

        this.msg = msg;
    }

    public static ResponseResult errorResult(int code,Boolean success, String msg) {
        ResponseResult result = new ResponseResult();

        return result.error(code, msg);
    }

    public static ResponseResult okResult(int code,Boolean success, String msg) {
        ResponseResult result = new ResponseResult();
        return result.ok(code, null, msg);
    }

    public static ResponseResult okResult(Object data) {
        ResponseResult result = setAppHttpCodeEnum(AppHttpCodeEnum.SUCCESS, AppHttpCodeEnum.SUCCESS.getErrorMessage());
        if (data != null) {
            result.setData(data);
        }
        return result;
    }

    public static ResponseResult errorResult(AppHttpCodeEnum enums) {
        return setAppHttpCodeEnum(enums, enums.getErrorMessage());
    }

    public static ResponseResult errorResult(AppHttpCodeEnum enums, String msg) {
        return setAppHttpCodeEnum(enums, msg);
    }

    public static ResponseResult setAppHttpCodeEnum(AppHttpCodeEnum enums) {
        return okResult(enums.getCode(),true, enums.getErrorMessage());
    }

    private static ResponseResult setAppHttpCodeEnum(AppHttpCodeEnum enums, String msg) {
        return okResult(enums.getCode(),true, msg);
    }

    public ResponseResult<?> error(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
        return this;
    }

    public ResponseResult<?> ok(Integer code, T data) {
        this.code = code;
        this.data = data;
        return this;
    }

    public ResponseResult<?> ok(Integer code, T data, String msg) {
        this.code = code;
        this.data = data;
        this.msg = msg;
        return this;
    }

    public ResponseResult<?> ok(T data) {
        this.data = data;
        return this;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getErrorMessage() {
        return msg;
    }

    public void setErrorMessage(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }


}